<?php

class PhotoImage
{

    /**
     * 判断图片类型,并返回。支持jpg,png,bmp,gif
     *
     * @param string $path 图片路径
     * @return boolean|string   失败返回false,成功返回图片扩展名
     */
    public static function getImageType($path)
    {
        $type = exif_imagetype($path);
        switch ($type) {
            case IMAGETYPE_JPEG :
                return 'jpg';
                break;
            case IMAGETYPE_GIF :
                return 'gif';
                break;
            case IMAGETYPE_PNG :
                return 'png';
                break;
            case IMAGETYPE_BMP :
                return 'bmp';
                break;
            default :
                return false;
        }
    }

    /**
     * 生成图片
     *
     * @param string $image_path 图片路径
     * @param int $new_width 缩略图的宽
     * @param int $new_height 缩略图的高
     * @param boolean $proportion 缩略图是否需要按比例，默认按比例
     * @param string $position 缩略图位置，默认top(top截取上面、middle截取中间、buttom截取下面)
     * @param int $quality 图片质量，默认80
     * @param boolean $watermark 是否需要水印，默认不需要
     * @param string $watermark_type 水印的方法，默认水印为LOGO，特殊水印需自己处理
     *
     * @return boolean                    返回生成图片的成功状态
     */
    public static function imageResizeSave(
        $image_path,
        $new_width,
        $new_height = 0,
        $proportion = true,
        $position = 'top',
        $quality = 85,
        $watermark = false,
        $watermark_type = '_watermark'
    ) {
        $image_type = self::getImageType($image_path);
        $image_type1 = substr($image_path, strripos($image_path, '.'));
        $dest_path = str_replace($image_type1, '_' . $new_width . $image_type1, $image_path);
        if ($image_type == 'gif') { // gif 不处理
            copy($image_path, $dest_path);
            return true;
        }
        if ($new_width == 1000) {
            $new_width = 960;
        } elseif ($new_width == 960) {
            $new_width = 640;
        }
        //从文件或URL中创建一图像
        switch ($image_type) {
            case 'jpg' :
                $image = imagecreatefromjpeg($image_path);
                break;
            case 'gif' :
                $image = imagecreatefromgif($image_path);
                break;
            case 'png' :
                $image = imagecreatefrompng($image_path);
                break;
            default :
                return false;
        }
        $image_size = getimagesize($image_path);
        $width = $image_size[0];
        $height = $image_size[1];
        $srcX = $srcY = 0;

        // 按比例生成图片，重新计算宽高和截取的起始位置
        if ($proportion == true) {
            // 原图尺寸小于缩略图尺寸
            if ($width <= $new_width) {
                $new_width = $width;
                $new_height = $height;
            } else {
                if ($new_width) {
                    $test_height = round($height * $new_width / $width);
                    $srcX = 0;
                    if ($position == 'middle') {
                        $srcY = round(($height - $test_height) / 2);
                    } elseif ($position == 'buttom') {
                        $srcY = round($height - $test_height);
                    }
                    $new_height = $test_height;
                } else {
                    $text_width = round($width * $new_height / $height);
                    if ($position == 'middle') {
                        $srcX = round(($width - $text_width) / 2);
                    } elseif ($position == 'buttom') {
                        $srcX = round($width - $text_width);
                    }
                    $srcY = 0;
                    $new_width = $text_width;
                }
            }
            if ($new_width < 1) {
                $new_width = 1;
            }
            if ($new_height < 1) {
                $new_height = 1;
            }
        }

        $image_color = imagecreatetruecolor($new_width, $new_height);
        $trans_colour = imagecolorallocate($image_color, 255, 255, 255);
        imagefill($image_color, 0, 0, $trans_colour);
        if (!imagecopyresampled($image_color, $image, 0, 0, $srcX, $srcY, $new_width, $new_height, $width, $height)) {
            return -1;
        }

        // 给图片信息加上水印信息
        if ($watermark == true) {
            if (method_exists('\Common\PhotoImage', $watermark_type)) {
                self::$watermark_type($image_color, $new_width, $new_height);
            } else {
                self::_watermark($image_color, $new_width, $new_height);
            }
        }

        // 输出图片信息到文件
        switch ($image_type) {
            case 'jpg' :
            case 'png' :
            case 'bmp' :
                if (imagejpeg($image_color, $dest_path, $quality)) {
                    imagedestroy($image_color);

                    return true;
                }
                break;
            case 'gif' :
                imagecolortransparent($image_color, imagecolorallocate($image_color, 0, 0, 0));
                if (imagegif($image_color, $dest_path)) {
                    imagedestroy($image_color);

                    return true;
                }
                break;
            default :
                return -2;
        }
        imagedestroy($image_color);

        return -3;
    }

    /**
     * 生成水印
     *
     * @param object $im 图片资源
     * @param string $width 图片资源的宽度
     * @param string $height 图片资源的高度
     *
     * @return object|boolean        图片资源
     */
    private static function _watermark(& $im, $width, $height)
    {
        global $app;
        $water_path = dirname($app->cfg['path']['root']) . '/public/images3/waterimg_min.png';
        if (file_exists($water_path)) {
            // 加载水印图片
            $water = imagecreatefrompng($water_path);
            if ($water) {
                return imagecopy($im, $water, $width - 20 - 150, $height - 40 - 57, 0, 0, 150, 57);
            }
        }

        return false;
    }

}
